#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *first_thread(void *arg)
{
    printf("first线程开始执行(%d), arg=%s \n", gettid(), (char *)arg);

    static int val = 3;
    return &val;
}

void *second_thread(void *arg)
{
    printf("second线程开始执行(%d) arg=%d \n", gettid(), *(int *)arg);

    return "hello";
}

int main(int argc, char const *argv[])
{
    printf("main线程(%d)开始执行 \n", gettid());

    // 创建分线程（自动会执行）
    pthread_t pt1, pt2;
    pthread_create(&pt1, NULL, first_thread, "atguigu");
    int val = 123;
    pthread_create(&pt2, NULL, second_thread, &val);
    printf("create之后main线程(%d)继续执行 pt1=%ld, pt2=%ld\n", gettid(), pt1, pt2);

    // main线程需要是最后一个结束
    // 等待分线程执行完毕
    int *result1;
    pthread_join(pt1, (void **)&result1);
    char *result2;
    pthread_join(pt2, (void **)&result2);
    
    printf("main线程(%d)结束 result2=%s result1=%d \n", gettid(), result2, *result1);
    return 0;
}
